package filter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyBrowserFilter implements Filter {

	protected FilterConfig config;

	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;// 从Web务器获取过滤器配置对象
	}

	@SuppressWarnings("deprecation")
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		String action = request.getServletPath();
		if(action.equals("/games/diceroll/rotate.html")){
			response.sendRedirect("/cloud_ifw/games/diceroll/rotate2.html?gameId=2&agentNo=8937");
			return;
		}
		chain.doFilter(req, resp);
	}

	public void destroy() {
	}

	public void writeFile(String serverUrl, String ip, String action)
			throws Exception {
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		String url = serverUrl + "assets/log";
		File path = new File(url);
		if (!path.exists())
			path.mkdirs();
		url = url + "/log" + y + "" + m + "" + d + ".txt";
		FileOutputStream fos = new FileOutputStream(url, true);
		String str = System.getProperty("line.separator") + action + "    ----[ " + ip + " ]";
		fos.write(str.getBytes());
		fos.close();
	}
}
